package com.beyondsoft.sabg.edu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.beyondsoft.sabg.edu.model.db.StudentInfo;
import com.beyondsoft.sabg.edu.model.db.TeacherInfo;
import com.beyondsoft.sabg.edu.model.dto.TeacherInfoDTO;
import com.beyondsoft.sabg.edu.repository.TeacherRepo;
import com.beyondsoft.sabg.edu.service.StudentService;
import com.beyondsoft.sabg.edu.service.TeacherService;
import com.beyondsoft.sabg.edu.service.core.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class TeacherInfoServiceImpl extends AbstractSimpleServiceImpl<TeacherInfo, TeacherRepo> implements TeacherService {

    @Autowired
    UserAccountService userAccountService;

    @Autowired
    StudentService studentService;


    @Override
    protected void addConditions(Root<TeacherInfo> root, CriteriaBuilder criteriaBuilder, List<Predicate> predicates, Object o) {
        super.addConditions(root, criteriaBuilder, predicates, o);
        if (o instanceof TeacherInfoDTO) {
            TeacherInfoDTO teacherInfoDTO = (TeacherInfoDTO) o;
            if (StrUtil.isNotEmpty(teacherInfoDTO.getTeacherNumber())) {
                predicates.add(criteriaBuilder.like(root.get("teacherNumber"), "%" + teacherInfoDTO.getTeacherNumber() + "%"));
            }

            if (StrUtil.isNotEmpty(teacherInfoDTO.getTeacherName())) {
                predicates.add(criteriaBuilder.like(root.get("teacherName"), "%" + teacherInfoDTO.getTeacherName() + "%"));
            }

            if (StrUtil.isNotEmpty(teacherInfoDTO.getTeacherCollegeId())) {
                predicates.add(criteriaBuilder.equal(root.get("teacherCollegeId"), Long.parseLong(teacherInfoDTO.getTeacherCollegeId())));
            }

            if (StrUtil.isNotEmpty(teacherInfoDTO.getTeacherCollege())) {
                predicates.add(criteriaBuilder.like(root.get("teacherCollege"), "%" + teacherInfoDTO.getTeacherCollege() + "%"));
            }
        }
    }

    @Override
    public TeacherInfo findByTeacherNumber(String number) {
        return getRepo().findByTeacherNumber(number);
    }

    @Override
    public Set<String> findCollegeList() {
        Set<String> strings = this.getRepo().findAll().stream()
                .collect(Collectors.groupingBy(teacherInfo -> teacherInfo.getTeacherCollege()))
                .keySet();


        return strings;
    }

    @Override
    public List<TeacherInfo> findByCollegeIdAndPosition() {
        String studentNumber = userAccountService.getCurrentUser().getUserName();
        StudentInfo byStudentNumber = studentService.getRepo().findByStudentNumber(studentNumber).get(0);
        return getRepo().findByTeacherPositionAndTeacherCollegeId(2, byStudentNumber.getCollegeID());
    }

    @Override
    public List<TeacherInfo> findPosition3(Long collegeID) {
        return getRepo().findByTeacherPositionAndTeacherCollegeId(3, collegeID);

    }
}
